package com.wfyn.reggie.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfyn.reggie.common.web.base.BaseController;
import com.wfyn.reggie.common.web.domain.Result;
import com.wfyn.reggie.modules.sys.domain.Category;
import com.wfyn.reggie.modules.sys.domain.Setmeal;
import com.wfyn.reggie.modules.sys.domain.SetmealDish;
import com.wfyn.reggie.modules.sys.param.DishDto;
import com.wfyn.reggie.modules.sys.param.PageListRequest;
import com.wfyn.reggie.modules.sys.param.SetmealDto;
import com.wfyn.reggie.modules.sys.service.CategoryService;
import com.wfyn.reggie.modules.sys.service.SetmealDishService;
import com.wfyn.reggie.modules.sys.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 * @author wfyn
 * @date 2023/9/28
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController extends BaseController {


        @Autowired
        SetmealService setmealService;

        @Autowired
        CategoryService categoryService;
        @Autowired
    SetmealDishService setmealDishService;

        /**
         *  套餐分页
         * @param request
         * @return
         */

        @GetMapping("/page")
        public Result pageList(PageListRequest request)
        {
            Page<Setmeal> setmealPage = new Page<>(request.getPage(), request.getPageSize());

            Page<SetmealDto> setmealDtoPage = new Page<>();


            setmealService.page(setmealPage, new LambdaQueryWrapper<Setmeal>()
                    .like(request.getName()!=null ,Setmeal::getName,request.getName())
                    .orderByDesc(Setmeal::getUpdateTime));


            BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");


            List<Setmeal> records = setmealPage.getRecords();

            List<SetmealDto> setmealDtos = records.
                    stream().map(
                    (item)->{
                        SetmealDto setmealDto = new SetmealDto();

                        BeanUtils.copyProperties(item,setmealDto);

                        Category category = categoryService.getById(item.getCategoryId());

                        setmealDto.setCategoryName( category.getName());

                        return setmealDto;

                    }
            ).collect(Collectors.toList());

            setmealDtoPage.setRecords(setmealDtos);
            return Result.success(setmealDtoPage);
        }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public Result saveSetmealDto(@RequestBody SetmealDto setmealDto)

    {
        setmealService.saveSetmealDto(setmealDto);
        return success();
    }

    /**
     * 更改起售状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam("ids") List<Long> ids) {


        setmealService.updateStatus(status,ids);

        return success();

    }

    @DeleteMapping
    public Result delete(@RequestParam("ids")List<Long> ids)
    {

            setmealService.deleteSetmealDto(ids);
            return success();


    }
   // http://localhost:8080/setmeal/list?categoryId=1397844391040167938&status=1
    @GetMapping("/list")
   public Result list(Setmeal setmeal)

    {


        return   success(
                setmealService.list(
                        new LambdaQueryWrapper<Setmeal>()
                                .eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId())
                                .eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus())
                                .orderByDesc(Setmeal::getUpdateTime)
                ));



    }

    @GetMapping("/dish/{id}")
    public Result get(@PathVariable Long id)
    {



        return success( setmealDishService.list(

                new LambdaQueryWrapper<SetmealDish>()
                        .eq(SetmealDish::getSetmealId,id)
        ));

    }
}




